package com.tssup.fesco.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.StringUtil;

/**
 * @author 海马
 * @description Json转换
 * @date 2016/7/25 0025
 * @see
 *
 */
@Deprecated
public class JsonUtil {
    public static String toJson(Object obj) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectMapper om = new ObjectMapper();
        JsonGenerator jsonGenerator = om.getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
        jsonGenerator.writeObject(obj);
        String json = outputStream.toString();
        outputStream.close();
        return json;
    }

    public static <T extends Object> T fromJson(String jason, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
        if (StringUtil.isEmpty(jason) || clazz == null) {
            return null;
        } else {
            ObjectMapper om = new ObjectMapper();
            T obj = om.readValue(jason, clazz);
            return obj;
        }
    }

    public static String mapToJson(Map<String, Object> map) throws IOException {
        return toJson(map);
    }

    public static Map<String, Object> jsonToMap(String jason) throws JsonParseException, JsonMappingException, IOException {
        if (StringUtil.isEmpty(jason)) {
            return null;
        }
        return (Map<String, Object>) fromJson(jason, Map.class);
    }

}
